﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.BLL.Analyse;
using WCFMINERP.COM;
using WCFMINERP.Contracts.InterFaces;
using WCFMINERP.MODEL;
using System.IO;

namespace WCFMINERP.Hosting
{
    public partial class FrmAutoOrder : Form
    {
        public FrmAutoOrder()
        {
            InitializeComponent();
        }
        AnalyseBLL _bll;
        AnalysPres _apBll;
        private void FrmAutoOrder_Load(object sender, EventArgs e)
        {


            _bll = new AnalyseBLL();



            _apBll = new AnalysPres();
            AnalysEventMonitor.AnalysInfoSended += AnalysInfoDispaly;
            //backgroundWorkerRunAnlyse.RunWorkerAsync();
            _bll.Start();
            //_bll.AnalyseOrder();
        }
        public void AnalysInfoDispaly(object sender, AnalysInfoEventArgs args)
        {
            if (args.EventType == EventType.Normal)
            {
                DisplayNormal(args.EventTime + ":" + args.Information, Color.Black);
            }
            else
            {
                DisplayNormal(args.EventTime + ":" + args.Information, Color.Red);

                DisplayError(args.EventTime + ":" + args.Information);
            }
            WriteAnalyseLog.WriteLogFile(args.EventTime + ":<" + args.EventType + ">" + args.Information);
        }

        public delegate void rushData(string strInfo, Color color);
        public delegate void rushErrorData(string strInfo);
        public void DisplayNormal(string strInfo, Color color)
        {
            if (richTextBoxNormal.InvokeRequired)
            {
                this.Invoke(new rushData(DisplayNormal), new object[] { strInfo, color });

            }
            else
            {
                strInfo += Environment.NewLine;
                richTextBoxNormal.Text = strInfo + richTextBoxNormal.Text;
                richTextBoxNormal.SelectionStart = 0;
                richTextBoxNormal.SelectionLength = strInfo.Length - 1;
                richTextBoxNormal.SelectionColor = color;
                richTextBoxNormal.SelectionLength = 0;
            }

        }
        public void DisplayError(string strInfo)
        {
            if (richTextBoxError.InvokeRequired)
            {
                this.Invoke(new rushErrorData(DisplayError), new object[] { strInfo });

            }
            else
            {
                strInfo += Environment.NewLine;
                richTextBoxError.Text = strInfo + richTextBoxError.Text;
                richTextBoxError.SelectionStart = 0;
                richTextBoxError.SelectionLength = strInfo.Length - 1;
                richTextBoxError.SelectionColor = Color.Red;
                richTextBoxError.SelectionLength = 0;
            }

        }

        private void tabControlInfomation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlInfomation.SelectedTab.Name == "tabPageErrList")
            {
                List<UploadQueue_Info> listuq = _apBll.GetAllErrList();
                gridControlUnUpload.DataSource = listuq;
                gridControlUnUpload.RefreshDataSource();

            }
        }
        UploadQueue_Info CurrSelect = null;
        private void toolStripButtonHandsOrder_Click(object sender, EventArgs e)
        {
            if (CurrSelect == null)
            {
                return;
            }
            ///UploadQueue_Info info= gridViewUnUpload.GetSelectedRows
            if (MessageBox.Show("确定要把" + CurrSelect.ShopName + "的" + CurrSelect.ZipName + "手动开单吗", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
              
                ///重命名 解压

                string Autopath = _apBll.GetPickUpOrderForder();//自动开单工作目录
                string savepath = Autopath.Substring(0, Autopath.LastIndexOf("\\") + 1) + "手动开单临时文件夹";//手动开单临时文件夹
                if (!Directory.Exists(savepath))
                {
                    Directory.CreateDirectory(savepath);
                }
                string dstFolder=Autopath + "\\" + CurrSelect.ZipName;
                string dstFile = Autopath + "\\" + CurrSelect.ZipName + ".zip";
                string strErr = "";
                if (Directory.Exists(dstFolder))
                {
                    bool isSucc = FileOperate.MoveDirectory(dstFolder, savepath, out strErr);//将文件夹移动走
                    if (isSucc)
                    {
                        if (File.Exists(dstFile))
                        {
                            File.Delete(dstFile);///删除ZIP文件
                        }

                    }
                    else
                    {
 
                    }
                }
                else
                {
                    MessageBox.Show("解压后文件不存在不能手动开单，请重新上传文件","",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                
                
              
                
                _apBll.SetCompleteById(CurrSelect.UploadId);
                // FileOperate.OpenFolderAndSelectedFile(
            }
        }

        private void toolStripButtonReUpload_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要把" + CurrSelect.ShopName + "的" + CurrSelect.ZipName + "重新上传吗？", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                int intVal=_apBll.SetOrderStatus(CurrSelect.UploadId, (int)TransferStatus.Wait);
                if (intVal != 0)
                {


                }
                else
                {
                    List<UploadQueue_Info> listuq = _apBll.GetAllErrList();
                    gridControlUnUpload.DataSource = listuq;
                    gridControlUnUpload.RefreshDataSource();
                }
            }
        }

        private void gridControlUnUpload_Click(object sender, EventArgs e)
        {
            
        }

        private void gridViewUnUpload_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                CurrSelect = (UploadQueue_Info)gridViewUnUpload.GetRow(e.RowHandle);
            }
        }

        private void FrmAutoOrder_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {

                e.Cancel = true;
                this.Hide();
            }
        }

        //private void backgroundWorkerRunAnlyse_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    _bll.AnalyseOrder();
        //}


    }
}
